package com.swan.controllers;

import java.util.List;
import java.util.Optional;

import com.swan.beans.SeasonBean;
import com.swan.models.SeasonDao;
import com.swan.utils.CellFactory;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 * SeasonMgtController
 * 
 * @author xyh
 * @param <T>
 * @date 20230426
 *
 */

public class SeasonMgtController {
	@FXML
	private Button DelBtn;

	@FXML
	private TableView<SeasonBean> TableView;

	@FXML
	private TextField NameTextField;

	@FXML
	private TableColumn<SeasonBean, Boolean> SelectTableColumn;

	@FXML
	private TableColumn<SeasonBean, String> NameTableColumn;

	private final ObservableList<SeasonBean> data = FXCollections.observableArrayList();

	private SeasonDao dao = new SeasonDao();

	@FXML
	public void onDelete(ActionEvent event) {
		delete();
	}

	@FXML
	public void onAdd(ActionEvent event) {
		if (NameTextField.getText() != null) {
			System.out.println(NameTextField.getText());

			String name = NameTextField.getText();

			SeasonBean row = new SeasonBean();
			row.setName(name);
			dao.addSeason(name);
//			if (row.getId() > 0) {
			System.out.println("add season success!");
			data.add(row);
//			}

			NameTextField.clear();
		}
	}

	private boolean delete() {
		int size = data.size();
		if (size <= 0) {
			return false;
		}
		for (int i = size - 1; i >= 0; i--) {
			SeasonBean row = data.get(i);
			if (row.getSelected()) {
				// confirm delete
				Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
				alert.setContentText("confirm delete season ?");
				Optional<ButtonType> showAndWait = alert.showAndWait();
				if (showAndWait.get() == ButtonType.OK) {
					data.remove(row);
					dao.removeSeason(row.getName());
					System.out.println("delete season success!");
				}
			}
		}
		return true;
	}

	@FXML
	private void initialize() {
		// table view
		// set select column ui
		SelectTableColumn
				.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
					@Override
					public ObservableValue<Boolean> call(Integer index) {
						final SeasonBean row = TableView.getItems().get(index);
						ObservableValue<Boolean> ret = new SimpleBooleanProperty(row, "selected", row.getSelected());
						ret.addListener(new ChangeListener<Boolean>() {
							@Override
							public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue,
									Boolean newValue) {
								row.setSelected(newValue);
							}
						});
						return ret;
					}
				}));

		// set column value
		SelectTableColumn.setCellValueFactory(new PropertyValueFactory<SeasonBean, Boolean>("selected"));
		NameTableColumn.setCellValueFactory(new PropertyValueFactory<SeasonBean, String>("name"));

		// set table view
		List<SeasonBean> list = dao.list();
		System.out.println("list season success!");
		data.addAll(list);

		TableView.setItems(data);
		TableView.setEditable(true);

	}

}
